/*
 * Copyright (C) 2015 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.madi.company.api.converter;

import com.google.gson.TypeAdapter;
import com.jaydenxiao.common.baserx.ApiException;
import com.madi.company.R;
import com.madi.company.api.exception.TokenInvalidException;
import com.madi.company.app.GlobalApplication;
import com.madi.company.bean.ApiModel;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;


final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, Object> {

    private final TypeAdapter<T> adapter;

    GsonResponseBodyConverter(TypeAdapter<T> adapter) {
        this.adapter = adapter;
    }

    @Override
    public Object convert(ResponseBody value) throws IOException {
        try {
            ApiModel apiModel = (ApiModel) adapter.fromJson(value.charStream());
            if (apiModel.code == 0) {
                return apiModel.data;
            }  else if (apiModel.code==3){
                throw new TokenInvalidException();
            }  else if (apiModel.code==2){
                throw new TokenInvalidException();
            }else if (apiModel.code==4) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code4));
            }else if (apiModel.code==5) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code5));
            }else if (apiModel.code==6) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code6));
            }else if (apiModel.code==7) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code7));
            }else if (apiModel.code==8) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code8));
            }else if (apiModel.code==9) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code9));
            }else if (apiModel.code==10) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code10));
            }else if (apiModel.code==11) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code11));
            }else if (apiModel.code==12) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code12));
            }else if (apiModel.code==13) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code13));
            }else if (apiModel.code==14) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code14));
            }else if (apiModel.code==15) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code15));
            }else if (apiModel.code==16) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code16));
            }else if (apiModel.code==20) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code20));
            }else if (apiModel.code==21) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code21));
            }else if (apiModel.code==22) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code22));
            }else if (apiModel.code==600) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code600));
            }else if(apiModel.code==1001){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1001));
            }else if(apiModel.code==1002){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1002));
            }else if(apiModel.code==1003){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1003));
            }else if(apiModel.code==1004){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1004));
            }else if(apiModel.code==1005){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1005));
            }else if(apiModel.code==1006){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1006));
            }else if(apiModel.code==1007){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1007));
            }else if(apiModel.code==1008){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1008));
            }else if(apiModel.code==1009){
                // 没看这个是什么玩意 等着错了再说 但是应该是有问题的这一块
                return apiModel.data;
            }else if(apiModel.code==1010){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1010));
            }else if(apiModel.code==1011){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1011));
            }else if(apiModel.code==1012){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1012));
            }else if(apiModel.code==1013){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1013));
            }else if(apiModel.code==1014){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1014));
            }else if(apiModel.code==1015){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1015));
            }else if(apiModel.code==1016){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1016));
            }else if (apiModel.code==1017) {
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1017));
            }else if(apiModel.code==1018){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1018));
            }else if(apiModel.code==1019){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1019));
            }else if(apiModel.code==1020){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1020));
            }else if(apiModel.code==1101){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.error_code1101));
            }else if(apiModel.code==23){
                throw new ApiException(GlobalApplication.getAppContext().getResources().getString(R.string.phonenumber_error));
            } else {
                throw new ApiException("错误码："+apiModel.code);
            }
//            else if (apiModel.errorCode == ErrorCode.TOKEN_INVALID) {
//                throw new TokenInvalidException();
//            } else
//            if (!apiModel.success) {
//                // 特定 API 的错误，在相应的 Subscriber 的 onError 的方法中进行处理
//                throw new ApiException();
//            } else if (apiModel.success) {
//                return apiModel.data;
//            }
        } finally {
            value.close();
        }
    }
}
